package cn.two.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;

import cn.two.model.OaCheck;
import cn.two.model.OaChecktypes;
import cn.two.model.OaDept;
import cn.two.model.OaSubmit;
import cn.two.model.OaUser;
import cn.two.model.Result;
import cn.two.service.OaCheckService;

@RestController
/**
 * 1.如果一个controller里面大部分返回的都是页面的话，使用@Controller，这时返回的都是页面
 * 2.如果大部分返回的都是Json或字符串的话，就使用@RestController。如果它碰到Stirng，就返回String
 * int返回int（即基本数据类型还返回相应的数据类型），但是对象会返回json
 * 3.此时(使用@RestController)如果某方法仍要返回视图，则使用ModelAndView
 */
public class OaCheckController {
	@Resource(name="oaCheckService")
	private OaCheckService oaCheckService;	
	public OaCheckService getOaCheckService() {
		return oaCheckService;
	}
	public void setOaCheckService(OaCheckService oaCheckService) {
		this.oaCheckService = oaCheckService;
	}

	/**
	 * 主视图
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpSession session) {
		
		return new ModelAndView("index");
	}
	
	/* 
	@RequestMapping("/getPage")
	public Map queryCheckByPage(int pageNum, int pageSize) {
		Page page = oaCheckService.queryChecksByPage(pageNum, pageSize);
		int total=(int) page.getTotal();
		int pages=page.getPages();
		Map  checkMap=new HashMap();
		checkMap.put("page",page);
		checkMap.put("total", total);
		checkMap.put("pages", pages);
		System.out.println(page);
		System.out.println("+++++++++++++++++++++++++++++++++"+total);
		System.out.println("================================"+pages);
		return checkMap;
	}*/
	
	/**
	 * 分页
	 * @param currPage
	 * @param pageSize
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/getPageByDynamic")
	public Map queryUsersByPage(int pageNum, int pageSize,OaCheck oaCheck,String pl,HttpServletRequest request) throws ParseException {	
		Map checkMap=new HashMap();
		String checkday = request.getParameter("checkday");
		checkMap.put("pageSize", pageSize);
		checkMap.put("pageNum", pageNum);
		checkMap.put("oaCheck", oaCheck);
		checkMap.put("checkday", checkday);
		
		System.out.println(checkday);
		
		String aString=oaCheck.getCheckTitle(); 
 		Page page=oaCheckService.queryLikeChecksByPage(checkMap);
 		int total=(int) page.getTotal();
		int pages=page.getPages();
		checkMap.put("page",page);
		checkMap.put("total", total);
		checkMap.put("pages", pages);
		return checkMap;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
	}
	
	
	/**
	 * 考勤列表
	 * @return
	 */
	@RequestMapping("/checklist")
	public ModelAndView checklist() {
		return new ModelAndView("checklist");
	}
	
	@RequestMapping("/checkaddpage")
	public ModelAndView checkaddpage() {
		return new ModelAndView("checkadd");
	}
	
	/**
	 * 考勤申请
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/checkadd")
	public ModelAndView checkadd(HttpSession session,String checkTime,Integer oaChecktypes,String checkBegin,String checkEnd,String checkDate,String checkTitle,String checkContext) throws Exception {
		OaUser oaUser = new OaUser();
		oaUser = (OaUser) session.getAttribute("oauser");
		OaDept oaDept=new OaDept();
		oaDept = (OaDept) session.getAttribute("oadept");
		OaChecktypes oaChecktype = new OaChecktypes();
		oaChecktype.setChecktypesId(oaChecktypes);
		OaSubmit oaSubmit= new OaSubmit();
		oaSubmit.setSubmitId(1);
		oaSubmit.setSubmitState("申请中");
		OaCheck oaCheck = new OaCheck();
		oaCheck.setOaUser(oaUser);
		oaCheck.setOaDept(oaDept);
		oaCheck.setOaChecktypes(oaChecktype);
		oaCheck.setOaSubmit(oaSubmit);
		
    	SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
    	Date datetime=sdf1.parse(checkTime);
    	oaCheck.setCheckTime(datetime);
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date datebegin=sdf.parse(checkBegin);
		oaCheck.setCheckBegin(datebegin);
		Date dateend=sdf.parse(checkEnd);
		oaCheck.setCheckEnd(dateend);
		oaCheck.setCheckDate(checkDate);
		oaCheck.setCheckTitle(checkTitle);
		oaCheck.setCheckContext(checkContext);	
		oaCheckService.addcheck(oaCheck);
		return new ModelAndView("checklist");
	}
	
	/**
	 * 查看单个考勤，跳转页面
	 * @param oaCheck
	 * @return
	 */
		@RequestMapping("/checkshow")
		public ModelAndView checkedit(OaCheck oaCheck) {
			OaCheck rsoaCheck = oaCheckService.checkshowbyid(oaCheck);
			Map<String, Object> rsmMap = new HashMap<String, Object>();
			rsmMap.put("data", rsoaCheck);
			return new ModelAndView("checkshow",rsmMap);
		}
	
		/**
		 * 编辑考勤，抓取数据，跳转页面
		 * @param oaCheck
		 * @return
		 */
	@RequestMapping("/checkeditshow")
	public ModelAndView checkeditshow(OaCheck oaCheck) {
		OaCheck rsoaCheck = oaCheckService.checkshowbyid(oaCheck);
		Map<String, Object> rsmMap = new HashMap<String, Object>();
		rsmMap.put("data", rsoaCheck);
		return new ModelAndView("checkedit",rsmMap);
	}

	/**
	 * 编辑考勤
	 * @param session
	 * @param checkId
	 * @param checkTime
	 * @param oaChecktypes
	 * @param checkBegin
	 * @param checkEnd
	 * @param checkDate
	 * @param checkTitle
	 * @param checkContext
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/checkedit")
	public Result checkedit(HttpSession session,int checkId,String checkTime,Integer oaChecktypes,String checkBegin,String checkEnd,String checkDate,String checkTitle,String checkContext) throws Exception {
		OaUser oaUser = new OaUser();
		oaUser = (OaUser) session.getAttribute("oauser");
		OaDept oaDept=new OaDept();
		oaDept = (OaDept) session.getAttribute("oadept");
		OaChecktypes oaChecktype = new OaChecktypes();
		oaChecktype.setChecktypesId(oaChecktypes);
		OaSubmit oaSubmit= new OaSubmit();
		oaSubmit.setSubmitId(1);
		oaSubmit.setSubmitState("申请中");
		OaCheck oaCheck = new OaCheck();
		oaCheck.setOaUser(oaUser);
		oaCheck.setOaDept(oaDept);
		
		
		oaCheck.setOaChecktypes(oaChecktype);
		oaCheck.setOaSubmit(oaSubmit);
		
    	SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
    	Date datetime=sdf1.parse(checkTime);
    	oaCheck.setCheckTime(datetime);
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date datebegin=sdf.parse(checkBegin);
		oaCheck.setCheckBegin(datebegin);
		Date dateend=sdf.parse(checkEnd);
		oaCheck.setCheckEnd(dateend);
		oaCheck.setCheckDate(checkDate);
		oaCheck.setCheckTitle(checkTitle);
		oaCheck.setCheckContext(checkContext);	
		oaCheck.setCheckId(checkId);
		oaCheckService.upcheck(oaCheck);
		return new Result();
	}
	
	
	/**
	 * 考勤列表
	 * @return
	 */
	@RequestMapping("/checkreplylist")
	public ModelAndView checkreplylist() {
		return new ModelAndView("checkreplylist");
	}
			/**
			 * 审批考勤，抓取数据，跳转页面
			 * @param oaCheck
			 * @return
			 */
		@RequestMapping("/checkreplyshow")
		public ModelAndView checkreplyshow(OaCheck oaCheck) {
			OaCheck rsoaCheck = oaCheckService.checkshowbyid(oaCheck);
			Map<String, Object> rsmMap = new HashMap<String, Object>();
			rsmMap.put("data", rsoaCheck);
			return new ModelAndView("checkreply",rsmMap);
		}
		/**
		 * 审批考勤
		 * @param session
		 * @param SubmitId
		 * @return
		 */
		@RequestMapping("/checkreply")
		public Result checkreply(OaCheck oaCheck,OaSubmit oaSubmit) {
			oaCheck.setOaSubmit(oaSubmit);
			oaCheckService.replycheck(oaCheck);
			return new Result();
		}
	
	/**
	 * 删除考勤
	 * @param oaCheck
	 * @return
	 */
	@RequestMapping("/checkdel")
	public Result checkdel(OaCheck oaCheck) {
		oaCheckService.delcheck(oaCheck);
		return new Result();
	}
	
}
